package com.codehacker.job.mallservice.controller;

import com.codehacker.job.mallservice.dao.AddressMapper;
import com.codehacker.job.mallservice.dto.ResponseDTO;
import com.codehacker.job.mallservice.entity.Address;
import com.codehacker.job.mallservice.entity.AddressExample;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * AddressController
 *
 * @author zhuyuwei
 * @date 26/07/2017
 */
@Controller
@RequestMapping("/api/address")
public class AddressController {
    @Resource
    private AddressMapper addressMapper;

    @ResponseBody
    public ResponseDTO<Address> getAll() {
        ResponseDTO<Address> result = new ResponseDTO<Address>();

        result.getMeta().setCode(0);
        return result;
    }

    @PostMapping("")
    @ResponseBody
    public ResponseDTO<Address> createAddress(@RequestBody Address address) {
        ResponseDTO<Address> result = new ResponseDTO<Address>();
        address.setCreateTime(new Date());
        address.setUpdateTime(new Date());
        addressMapper.insertSelective(address);
        result.getMeta().setCode(0);
        return result;
    }

    @GetMapping("/{id}")
    @ResponseBody
    public ResponseDTO<Address> findById(@PathVariable("id") Integer id) {
        ResponseDTO<Address> result = new ResponseDTO<Address>();
        result.getData().getItems().add(addressMapper.selectByPrimaryKey(id));
        result.getMeta().setCode(0);
        return result;
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public ResponseDTO<Address> deleteById(@PathVariable("id") Integer id) {
        ResponseDTO<Address> result = new ResponseDTO<Address>();
        addressMapper.deleteByPrimaryKey(id);
        result.getMeta().setCode(0);
        return result;
    }

    @GetMapping("/default")
    @ResponseBody
    public ResponseDTO<Address> getDefaultAddress(@RequestParam("uid") Integer uid) {
        ResponseDTO<Address> result = new ResponseDTO<Address>();
        AddressExample example = new AddressExample();
        example.createCriteria().andIsDefEqualTo(0).andUserIdEqualTo(uid);
        result.getData().getItems().addAll(addressMapper.selectByExample(example));
        result.getMeta().setCode(0);
        return result;
    }

    @PostMapping("/default/{id}")
    @ResponseBody
    public ResponseDTO<Address> setDefaultAddress(@PathVariable("id") Integer id) {
        ResponseDTO<Address> result = new ResponseDTO<Address>();
        Address address = addressMapper.selectByPrimaryKey(id);
        address.setIsDef(1);
        addressMapper.updateByPrimaryKey(address);
        result.getMeta().setCode(0);
        return result;
    }

    @GetMapping("")
    @ResponseBody
    public ResponseDTO<Address> pageing(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        ResponseDTO<Address> result = new ResponseDTO<Address>();
        AddressExample example = new AddressExample();
        example.setOrderByClause("create_time desc");
        result.getData().getItems().addAll(addressMapper.selectByExampleWithPaging(example, (page - 1) * limit, limit));
        result.getData().getPaginate().setTotal((int)addressMapper.countByExample(example));
        result.getData().getPaginate().setHasNext(result.getData().getPaginate().getTotal() < (page + 1)*limit ? false: true);
        result.getData().getPaginate().setNext(page + 1);
        result.getData().getPaginate().setPerPage(limit);
        result.getMeta().setCode(0);
        return result;
    }
}
